package com.op.conventer;

import com.op.constants.OtherConstant;
import com.op.converter.QuestionConverter;
import com.op.entity.UserQuestionData;
import com.op.model.dto.BankCorrectDto;
import com.op.model.dto.DifficultyDataDto;
import com.op.model.vo.AllCountByUserVo;
import com.op.model.vo.BankCorrectVo;
import com.op.model.vo.DifficultyDataVo;
import com.op.model.vo.XQuestionDataVo;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName： UserQuestionDataConverter
 * Date： 2021/11/2 15:43
 *
 * @author 孤焰
 * @version 1.0
 **/

public class UserQuestionDataConverter {

    /**
     * 将用户题目列表转换为数据累加Vo
     * @param userQuestionDataList 用户题目列表
     * @return 数据累加Vo
     */
    public static AllCountByUserVo convertDataListToAllCountVo(List<UserQuestionData> userQuestionDataList) {
        AllCountByUserVo allCountByUserVo = new AllCountByUserVo();
        userQuestionDataList
                .forEach(userQuestionData -> {
                    allCountByUserVo.setCorrectCount(allCountByUserVo.getCorrectCount() + userQuestionData.getCorrectCount());
                    allCountByUserVo.setUncertainCount(allCountByUserVo.getUncertainCount() + userQuestionData.getUncertainCount());
                    allCountByUserVo.setMistakeCount(allCountByUserVo.getMistakeCount() + userQuestionData.getMistakeCount());
                    allCountByUserVo.setAnswerCount(allCountByUserVo.getAnswerCount() + userQuestionData.getAnswerCount());
                });
        return allCountByUserVo;
    }

    /**
     * 将题库正确率Dto转换为Vo类型
     * @param bankCorrectDtoList 题库正确率Dto列表
     * @return 题库正确率Vo
     */
    public static BankCorrectVo convertBankCorrectDtoToVo(List<BankCorrectDto> bankCorrectDtoList) {
        List<String> bankNameList = new ArrayList<>(bankCorrectDtoList.size());
        List<Double> correctList = new ArrayList<>(bankCorrectDtoList.size());
        bankCorrectDtoList.forEach(bankCorrectDto -> {
            bankNameList.add(bankCorrectDto.getBankName());
            correctList.add(
                    BigDecimal.valueOf((double) bankCorrectDto.getCorrectCount() / bankCorrectDto.getAnswerCount() * 100)
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue());
        });
        return new BankCorrectVo(bankNameList, correctList);
    }

    /**
     * 将用户题目数据列表转换为Vo类型
     * @param userQuestionDataList 用户题目数据列表
     * @return 用户题目数据Vo
     */
    public static XQuestionDataVo convertDataListToVo(List<UserQuestionData> userQuestionDataList) {
        List<String> modifyTimeList = new ArrayList<>(userQuestionDataList.size());
        List<Double> correctList = new ArrayList<>(userQuestionDataList.size());
        List<Double> uncertainList = new ArrayList<>(userQuestionDataList.size());
        List<Double> mistakeList = new ArrayList<>(userQuestionDataList.size());
        SimpleDateFormat sdf =new SimpleDateFormat(OtherConstant.DATE_FORMAT);
        userQuestionDataList.forEach(userQuestionData -> {
            modifyTimeList.add(sdf.format(userQuestionData.getModifyTime()));
            correctList.add(
                    BigDecimal.valueOf((double) userQuestionData.getCorrectCount() / userQuestionData.getAnswerCount() * 100)
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue());
            uncertainList.add(
                    BigDecimal.valueOf((double) userQuestionData.getUncertainCount() / userQuestionData.getAnswerCount() * 100)
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue());
            mistakeList.add(
                    BigDecimal.valueOf((double) userQuestionData.getMistakeCount() / userQuestionData.getAnswerCount() * 100)
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue());
        });
        return new XQuestionDataVo(modifyTimeList, correctList, uncertainList, mistakeList);
    }

    /**
     * 将难易度问答数据Dto转换为Vo类型
     * @param difficultyDataDtoList 难易度问答数据Dto列表
     * @return 难易度问答数据Vo
     */
    public static DifficultyDataVo convertDifficultyDataDtoToVo(List<DifficultyDataDto> difficultyDataDtoList) {
        List<String> difficultyList = new ArrayList<>(difficultyDataDtoList.size());
        List<Double> correctList = new ArrayList<>(difficultyDataDtoList.size());
        List<Double> uncertainList = new ArrayList<>(difficultyDataDtoList.size());
        List<Double> mistakeList = new ArrayList<>(difficultyDataDtoList.size());
        difficultyDataDtoList.forEach(difficultyDataDto -> {
            difficultyList.add(QuestionConverter.convertDiffIntToString(difficultyDataDto.getDifficulty()));
            correctList.add(
                    BigDecimal.valueOf((double) difficultyDataDto.getCorrectCount() / difficultyDataDto.getAnswerCount() * 100)
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue());
            uncertainList.add(
                    BigDecimal.valueOf((double) difficultyDataDto.getUncertainCount() / difficultyDataDto.getAnswerCount() * 100)
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue());
            mistakeList.add(
                    BigDecimal.valueOf((double) difficultyDataDto.getMistakeCount() / difficultyDataDto.getAnswerCount() * 100)
                            .setScale(2, BigDecimal.ROUND_HALF_UP)
                            .doubleValue());
        });
        return new DifficultyDataVo(difficultyList, correctList, uncertainList, mistakeList);
    }
}
